<?php

class AuthController extends HTech_Controller_Action
{   
    
    public function init()
    {
    }

    public function loginAction()
    {
        $this->view->form = new Emms_Form_Default_Login(); 
        $this->view->formPwdRecovery = new Emms_Form_Default_PwdRecovery();            
        $this->view->localeOpt = HTech_Locale::getLocaleOpt();
        $this->view->clName = Zend_Registry::get('clName');
        
        if ($this->getRequest()->isPost()) {
            
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
              
                $cl = $this->getParam('cl'); 
                $db = $this->getParam('db');        
                $usr = $this->getParam('username');
                $pwd = $this->getParam('password');
                /*
                 * Try login using regular password
                 */
                $credTreatment = sprintf('md5(?) and Status = "Active" 
                    and SystemAccessFromDate <= "%s" 
                    and SystemAccessToDate >= "%s"', date("Y-m-d"), date("Y-m-d"));
                $login = new HTech_Login($cl,$db,$usr,$pwd,'view_users','Username','Password',$credTreatment);
                if ($login->authenticate()->isValid()) { 
                    /*
                     *  Success! Store user profile to auth's storage system.
                     */  
                    $login->saveProfile();
                    Zend_Session::namespaceUnset('Report');
                    $this->_redirect('default/index/index');
                    exit;
                } 
                /*
                 * Try login using a temporary password
                 */
                $tmpCredTreatment = sprintf('? and Status = "Active" 
                    and SystemAccessFromDate <= "%s" 
                    and SystemAccessToDate >= "%s"
                    and TmpPasswordDatetime >= "%s"', date("Y-m-d"), date("Y-m-d"), date("Y-m-d H:i:s"));
                $tmpLogin = new HTech_Login($cl,$db,$usr,$pwd,'view_users','Username','TmpPassword',$tmpCredTreatment);
                if ($tmpLogin->authenticate()->isValid()) {
                    /*
                     *  Success! Store user profile to auth's storage system.
                     */  
                    $tmpLogin->saveProfile();
                    $this->_redirect('default/index/index');
                    exit;
                } 
            } 
            $this->view->invalidLogin = true;
        }
        
        $this->view->conf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini');
        $this->view->userAgent = new Zend_Http_UserAgent();
        
        $this->_helper->layout->setLayout('login');
    }

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $cl = @ Zend_Registry::get('emmsClient');
        $locale = @ Zend_Registry::get('Zend_Locale');
        $this->_redirect(sprintf('/%s?locale=%s',$cl,$locale));
    }

    public function denyAction()
    {
        $this->_helper->layout->setLayout('view'); 
        
        $this->view->title = $this->view->translate("Denied access");
        $this->view->subtitle = $this->view->translate("Your request could not be completed");
        $this->view->message = $this->getHelper('flashMessenger')->getMessages();
    }

    public function infoAction()
    {
        $this->_helper->layout->setLayout('view'); 
        
        $this->view->title = $this->view->translate("INFO");
        $this->view->subtitle = "";
        $this->view->message = $this->getHelper('flashMessenger')->getMessages();
    }
    
    public function expiredAction()
    {
        $this->_helper->layout->setLayout('expired'); 
    }
}

